typedef struct _BMP_FILE_HEADER_T{
	UINT8 sig[2];
	UINT8 len[4];
	UINT8 data0[12];
	UINT8 width[4];
	UINT8 height[4];
	UINT8 data1[46];
}BMP_FILE_HEADER_T;

static UINT8 bmp_header[]={
		0x42,0x4D,0x48,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x48,0x00,0x00,0x00,0x28,0x00,
		0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x06,
		0x00,0x00,0x01,0x00,0x10,0x00,0x03,0x00,
		0x00,0x00,0x00,0x80,0x00,0x00,0xD0,0x0E,
		0x00,0x00,0xD0,0x0E,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,
		0x00,0x00,0xE0,0x07,0x00,0x00,0x1F,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};//bmp header for RGB565

void yuv2rgb_565(UINT8 *yuv,UINT8 *rgb)
{
	long int Y,U,V,R,G,B;//i;
	int uvr, uvg, uvb;

	U=(long int)*yuv++;
	Y=(long int)*yuv++;
	V=(long int)*yuv++;
#if 1	
	R=Y+1402*(V-128)/1000;
	G=Y-(34414*(U-128)+71414*(V-128))/100000;
	B=Y+1772*(U-128)/1000;
	if (R>0xFF) R=0xFF;
	if (G>0xFF) G=0xFF;
	if (B>0xFF) B=0xFF;
	if (R<0) R=0;
	if (G<0) G=0;
	if (B<0) B=0;

	*((UINT16*)rgb) = ((B>>3)&0x1F) + (((G>>2)&0x3F)<<5) + (((R>>3)&0x1F)<<11);
	
	Y=(long int)*yuv++;
	R=Y+1402*(V-128)/1000;
	G=Y-(34414*(U-128)+71414*(V-128))/100000;
	B=Y+1772*(U-128)/1000;
	if (R>0xFF) R=0xFF;
	if (G>0xFF) G=0xFF;
	if (B>0xFF) B=0xFF;
	if (R<0) R=0;
	if (G<0) G=0;
	if (B<0) B=0;

	*((UINT16*)rgb + 1) = ((B>>3)&0x1F) + (((G>>2)&0x3F)<<5) + (((R>>3)&0x1F)<<11);
#else
			uvr=18*U+367*V;
			uvg=159*U+220*V;
			uvb=411*U-29*V;

			R=(Y+uvr)>>8;
			G=(Y-uvg)>>8;
			B=(Y+uvb)>>8;
	if (R>0xFF) R=0xFF;
	if (G>0xFF) G=0xFF;
	if (B>0xFF) B=0xFF;
	if (R<0) R=0;
	if (G<0) G=0;
	if (B<0) B=0;
	*((UINT16*)rgb) = ((R>>3)&0x1F) + (((G>>2)&0x3F)<<5) + (((B>>3)&0x1F)<<11);

			Y=(long int)*yuv++;
			R=(Y+uvr)>>8;
			G=(Y-uvg)>>8;
			B=(Y+uvb)>>8;
	if (R>0xFF) R=0xFF;
	if (G>0xFF) G=0xFF;
	if (B>0xFF) B=0xFF;
	if (R<0) R=0;
	if (G<0) G=0;
	if (B<0) B=0;
	*((UINT16*)rgb + 1) = ((R>>3)&0x1F) + (((G>>2)&0x3F)<<5) + (((B>>3)&0x1F)<<11);
#endif
}

void img_yuv2rgb565(UINT32 *yuv, int w, int h)
{
	int i;
	
	for (i=0; i<w*h/2; i++)
		yuv2rgb_565((UINT8*)&yuv[i], (UINT8*)&yuv[i]);
}

void generate_bmp_header()
{
	#if 0
		p_bmp_header->len[0]=((IMG_W*IMG_H+0x46)>> 0)&0xFF;
		p_bmp_header->len[1]=((IMG_W*IMG_H+0x46)>> 8)&0xFF;
		p_bmp_header->len[2]=((IMG_W*IMG_H+0x46)>>16)&0xFF;
		p_bmp_header->len[3]=((IMG_W*IMG_H+0x46)>>24)&0xFF;
		
		p_bmp_header->width[0]=(IMG_W>> 0)&0xFF;
		p_bmp_header->width[1]=(IMG_W>> 8)&0xFF;
		p_bmp_header->width[2]=(IMG_W>>16)&0xFF;
		p_bmp_header->width[3]=(IMG_W>>24)&0xFF;
		
		p_bmp_header->height[0]=(IMG_H>> 0)&0xFF;
		p_bmp_header->height[1]=(IMG_H>> 8)&0xFF;
		p_bmp_header->height[2]=(IMG_H>>16)&0xFF;
		p_bmp_header->height[3]=(IMG_H>>24)&0xFF;
#endif
}
